package com.icebergteam.warfacewidget.core.settings;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;

import com.icebergteam.warfacewidget.AppManager;
import com.icebergteam.warfacewidget.R;
import com.icebergteam.warfacewidget.core.LogManager;

public class FinderSettings extends Settings {
	public static final String className = GeneralSettings.class.getCanonicalName();
	
	public final static String PREFS_FILE_NAME = "finder_settings.ini";
	// Request
	private boolean showAuticompleteList	= true;
	private boolean alwaysUseCurrentRegion	= false;
	// Response
	private final int MIN_RESULT_ITEMS_COUNT_ID = 0;
	private final int DEFAULT_RESULT_ITEMS_COUNT_ID = 1;
	private final int MAX_RESULT_ITEMS_COUNT_ID = 2;	
	private final int RESULT_ITEMS_RANGE[] = { 5, 20, 100 };
	private int resultItemsCount = RESULT_ITEMS_RANGE[DEFAULT_RESULT_ITEMS_COUNT_ID];
	
	public boolean isShowAuticompleteList() {
		return showAuticompleteList;
	}
	public void setShowAuticompleteList(boolean showAuticompleteList) {
		this.showAuticompleteList = showAuticompleteList;
	}
	public boolean isAlwaysUseCurrentRegion() {
		return alwaysUseCurrentRegion;
	}
	public void setAlwaysUseCurrentRegion(boolean alwaysUseCurrentRegion) {
		this.alwaysUseCurrentRegion = alwaysUseCurrentRegion;
	}
	public int getResultItemsCount() {
		return resultItemsCount;
	}
	public void setResultItemsCount(int resultItemsCount) {
		this.resultItemsCount = resultItemsCount;
	}
	
	public int getMinResultItemsCount() {
		return RESULT_ITEMS_RANGE[MIN_RESULT_ITEMS_COUNT_ID];
	}
	
	public int getMaxResultItemsCount() {
		return RESULT_ITEMS_RANGE[MAX_RESULT_ITEMS_COUNT_ID];
	}
	
	public FinderSettings() {
		Context context = AppManager.getContext();
		init(context);
		load();
	}

	@Override
	public void save() {
		Context context = AppManager.getContext();
		try {
			FileOutputStream fos = context.openFileOutput(getPrefFileName(),
					Context.MODE_PRIVATE);
			this.properties.setProperty(
					getKey(R.string.key_prefs_finder_request_show_autocomplete_list),
					String.format("%s", this.showAuticompleteList));
			this.properties.setProperty(
					getKey(R.string.key_prefs_finder_request_always_use_default_region),
					String.format("%s", this.alwaysUseCurrentRegion));
			this.properties.setProperty(
					getKey(R.string.key_prefs_finder_response_result_items_count),
					String.format("%s", this.resultItemsCount));
			
			this.properties.store(fos, "wotwidget");
			fos.close();
			LogManager.LogV(NewsSettings.className, LogManager.getExecuteMethodName(), this.showAuticompleteList,
					this.alwaysUseCurrentRegion, this.resultItemsCount, this.properties.toString());
		} catch (FileNotFoundException e) {
			LogManager.LogE(NewsSettings.className, LogManager.getExecuteMethodName() + " can't read pref file",
					e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			LogManager.LogE(NewsSettings.className, LogManager.getExecuteMethodName() + " can't read pref file",
					e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
			LogManager.LogE(NewsSettings.className, LogManager.getExecuteMethodName(),
					e.getMessage());
			e.printStackTrace();
		}
	}

	@Override
	public void load() {
		String showAuticompleteList = this.properties.getProperty(
				getKey(R.string.key_prefs_finder_request_show_autocomplete_list),
				String.format("%s", this.showAuticompleteList));
		String alwaysUseCurrentRegion = this.properties.getProperty(
				getKey(R.string.key_prefs_finder_request_always_use_default_region),
				String.format("%s", this.alwaysUseCurrentRegion));
		String resultItemsCount = this.properties.getProperty(
				getKey(R.string.key_prefs_finder_response_result_items_count),
				String.format("%s", this.resultItemsCount));
		
		try {
			this.showAuticompleteList		= Boolean.parseBoolean(showAuticompleteList);
			this.alwaysUseCurrentRegion		= Boolean.parseBoolean(alwaysUseCurrentRegion);
			this.resultItemsCount			= Integer.parseInt(resultItemsCount);

			LogManager.LogV(NewsSettings.className, LogManager.getExecuteMethodName(), this.showAuticompleteList,
					this.alwaysUseCurrentRegion, this.resultItemsCount, this.properties.toString());
		} catch (Exception e) {
			LogManager.LogE(NewsSettings.className, LogManager.getExecuteMethodName(), e.getMessage());
			e.printStackTrace();
		}
	}
	@Override
	public String getPrefFileName() {
		return FinderSettings.PREFS_FILE_NAME;
	}
}
